package cn.com.xf.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;
import cn.com.xf.dao.TblOaDutyDao;
import cn.com.xf.dao.support.XFSqlMapClientDaoSupport;
import cn.com.xf.domain.Tbempinfo;
import cn.com.xf.domain.TblOaDuty;
import cn.com.xf.domain.TblOaDutySchedule;

@SuppressWarnings("unchecked")
@Repository
public class TblOaDutyDaoImpl extends XFSqlMapClientDaoSupport implements TblOaDutyDao {

	@Override
	public List<TblOaDuty> selectDutys(TblOaDuty form) {

		List<TblOaDuty> list = getSqlMapClientTemplate().queryForList("TBL_OA_DUTY.selectDutys", form);
		return list;
	}

	@Override
	public int selectDutyCount(String siteId, String deptId, Date dutyDate) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("siteid", siteId);
		map.put("dutydate", dutyDate);
		map.put("deptid", deptId);
		return (Integer) getSqlMapClientTemplate().queryForObject("TBL_OA_DUTY.selectCount", map);
	}
	//构造器
	public TblOaDutyDaoImpl() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<TblOaDuty> select(int begin, int pageSize, String siteId, String deptId, Date dutyDate) {
		Map<String, Object> map = new HashMap<String, Object>();
		int end = begin + pageSize;
		begin = begin + 1;
		map.put("begin", begin);
		map.put("end", end);
		map.put("siteid", siteId);
		map.put("deptid", deptId);
		map.put("dutydate", dutyDate);
		return getSqlMapClientTemplate().queryForList("TBL_OA_DUTY.selectPageDuty", map);
	}

	@Override
	public TblOaDuty selectDutyById(String id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		return (TblOaDuty) getSqlMapClientTemplate().queryForObject(
				"TBL_OA_DUTY.selectDutyById", map);
	}

	@Override
	public String selectDeptIdByUser(String name) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		String deptId = (String)getSqlMapClientTemplate().queryForObject(
				"TBL_OA_DUTY.selectDeptIdByUser", map);
		return deptId;
	}

	@Override
	public int updateDuty(TblOaDuty duty) {
		int rows = getSqlMapClientTemplate().update("TBL_OA_DUTY.updateDuty",duty);
		return rows;
	}

	@Override
	public int deleteById(String dutyId) {
		TblOaDuty key = new TblOaDuty();
		key.setDutyId(dutyId);
		int rows = getSqlMapClientTemplate().delete("TBL_OA_DUTY.deleteDuty", key);
		return rows;
		
	}

	@Override
	public int insert(TblOaDuty duty) {
		getSqlMapClientTemplate().insert("TBL_OA_DUTY.insertDuty", duty);
		return 1;
	}

	@Override
	public List<TblOaDutySchedule> selectSchedules(String siteId, String deptId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("siteId", siteId);
		map.put("deptId", deptId);
		return getSqlMapClientTemplate().queryForList("TBL_OA_DUTY.selectSchedules", map);
	}

	@Override
	public TblOaDuty selectTelNo(String userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", userId);
		return (TblOaDuty)getSqlMapClientTemplate().queryForObject("TBL_OA_DUTY.selectTelNo", map);
	}

	@Override
	public int updateUserTelNo(String userId, String userTelNo) {
		HashMap<String, Object> TelNoMap = new HashMap<String,Object>();
		TelNoMap.put("userId", userId);
		TelNoMap.put("userTelNo", userTelNo);
		int rows = getSqlMapClientTemplate().update("TBL_OA_DUTY.updateUserTelNo",TelNoMap);
		return rows;
	}

	@Override
	public TblOaDutySchedule selectScheduleById(String id) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", id);
			return (TblOaDutySchedule) getSqlMapClientTemplate().queryForObject(
					"TBL_OA_DUTY.selectScheduleById", map);
	}

	@Override
	public TblOaDutySchedule selectDeptTelNo(String siteId, String deptId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("siteId", siteId);
		map.put("deptId", deptId);
		List<TblOaDutySchedule> List = (List<TblOaDutySchedule>)getSqlMapClientTemplate().queryForList("TBL_OA_DUTY.selectDeptTelNo", map);
		return List.get(0);
	}

	@Override
	public int updateSchedule(TblOaDutySchedule schedule) {
		int rows = getSqlMapClientTemplate().update("TBL_OA_DUTY.updateSchedule",schedule);
		return rows;
		
	}

	@Override
	public int updateDeptTelNo(String siteId, String deptId, String deptTelNo) {
		HashMap<String, Object> TelNoMap = new HashMap<String,Object>();
		TelNoMap.put("siteId", siteId);
		TelNoMap.put("deptId", deptId);
		TelNoMap.put("deptTelNo", deptTelNo);
		int rows = getSqlMapClientTemplate().update("TBL_OA_DUTY.updateDeptTelNo",TelNoMap);
		return rows;
	}

	@Override
	public int insertSchedule(TblOaDutySchedule schedule) {
		getSqlMapClientTemplate().insert("TBL_OA_DUTY.insertSchedule", schedule);
		return 1;
	}

	@Override
	public int deleteScheduleById(String idStr) {
		Date deleteDate = new Date();
		HashMap<String, Object> paramMap = new HashMap<String,Object>();
		paramMap.put("scheduleId",idStr);
		paramMap.put("deleteDate", deleteDate);
		int rows = getSqlMapClientTemplate().delete("TBL_OA_DUTY.deleteSchedule", paramMap);
		int rows2 = getSqlMapClientTemplate().delete("TBL_OA_DUTY.deleteDutysBySchedule", paramMap);
		return rows+rows2;
	}
	//勿改为JAVA命名规范，为了和原来兼容
	@Override
	public List<Tbempinfo> selectGetDept(String siteId) {
		return getSqlMapClientTemplate().queryForList("TBEMPINFO.selectGetDept", siteId);
	}
	//勿改为JAVA命名规范，为了和原来兼容
	@Override
	public List<Tbempinfo> selectEmpBySiteidWithoutDept(String siteId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("siteid", siteId);
		return getSqlMapClientTemplate().queryForList("TBEMPINFO.selectEmpListBySiteidWithoutDept", map);
	}
	//勿改为JAVA命名规范，为了和原来兼容
	@Override
	public List<Tbempinfo> selectEmpBySiteIdAndDeptid(String siteId, String deptId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("siteid", siteId);
		map.put("deptid", deptId);
		return getSqlMapClientTemplate().queryForList("TBEMPINFO.selectEmpListBySiteIdAndDeptid", map);
	}

	@Override
	public String selectSiteIdByUser(String empId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("empId", empId);
		return (String) getSqlMapClientTemplate().queryForObject("TBL_OA_DUTY.selectSiteIdByUser", map);
	}

	@Override
	public List<TblOaDuty> copyDutys(TblOaDuty duty) {
		List<TblOaDuty> list = getSqlMapClientTemplate().queryForList("TBL_OA_DUTY.copyDutys", duty);
		return list;
	}
	
}
